/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.ui.editors;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.zoundry.gaedt.core.models.application.ZGaeApplicationModel;
import org.zoundry.gaedt.core.util.ZGaeUtil;
import org.zoundry.gaedt.ui.editors.operations.IZGaeUndoableOperation;

/**
 * An abstract base class for application editor pages.
 */
public abstract class ZGaeAbstractApplicationEditorPage extends FormPage implements IZGaeApplicationEditorPage
{
    /**
     * C'tor.
     *
     * @param aEditor
     * @param aId
     * @param aTitle
     */
    public ZGaeAbstractApplicationEditorPage(FormEditor aEditor, String aId, String aTitle)
    {
        super(aEditor, aId, aTitle);
    }

    /**
     * @see org.zoundry.gaedt.ui.editors.IZGaeApplicationEditorPage#getParentEditor()
     */
    public ZGaeApplicationEditor getParentEditor()
    {
        return ((ZGaeApplicationEditor) getEditor());
    }

    /**
     * Executes the given operation in the editor's operation history.
     *
     * @param aOperation
     */
    protected void executeOperation(IZGaeUndoableOperation aOperation)
    {
        if (!aOperation.shouldExecute())
            return;

        try
        {
            aOperation.addContext(getUndoContext());
            getOperationHistory().execute(aOperation, null, getEditor());
            getEditor().editorDirtyStateChanged();
        }
        catch (ExecutionException e)
        {
            ZGaeUtil.logError(e);
        }
    }

    /**
     * Convenience method for getting the model.
     */
    protected ZGaeApplicationModel getModel()
    {
        return getParentEditor().getModel();
    }

    /**
     * Gets the undo context to use for operations.
     */
    protected IUndoContext getUndoContext()
    {
        return getParentEditor().getUndoContext();
    }

    /**
     * Gets the operation history.
     */
    protected IOperationHistory getOperationHistory()
    {
        return getSite().getWorkbenchWindow().getWorkbench().getOperationSupport().getOperationHistory();
    }
}
